package at.htlkaindorf.sportartikel.webshop.view;

import at.htlkaindorf.sportartikel.dao.api.ProductDao;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.ExpressionFactory;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Thomas
 */
@ManagedBean(name = "productListBean")
@ViewScoped
public class ProductListBean implements Serializable {

    private ProductDao productDao;

    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
        ELContext elContext = context.getELContext();
        
        ValueExpression fetchCountry = expressionFactory.createValueExpression(elContext, "#{sessionBean.country}", String.class);
        String country = (String) fetchCountry.getValue(elContext);

        ValueExpression fetchProductDao = expressionFactory.createValueExpression(elContext, "#{ProductDao" + country + "}", ProductDao.class);
        productDao = (ProductDao) fetchProductDao.getValue(elContext);
    }

    public String hello() {
        System.out.println(productDao);
        return productDao.toString();
    }

    public ProductDao getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

}
